/*create By_lxyea*/
/*create Date 2017-09-19*/
/*import M from'./common';
 */
const jsonp = require('jsonp');
import api from './common';
/**
 * [wechat 信息]
 */
const wechat = {
    src_JSDK: process.env.src_JSDK,
    sid: process.env.sid,
    _appid: process.env._appid,
    swechatframework_url: process.env.swechatframework_url,
    redirect_uri: process.env.redirect_uri,
    redirect_ada: process.env.redirect_ada
};
// (function () {
//     var oHead = document.getElementsByTagName('head').item(0);
//     var oScript = document.createElement("script");
//     oScript.type = "text/javascript";
//     var localUrl = window.location.href;
//     if (localUrl.indexOf("#") >= 0) {
//         localUrl = localUrl.substring(0, localUrl.indexOf("#"));
//     }
//     oScript.src = wechat.src_JSDK + '?sid=' + wechat.sid + '&url=' + encodeURIComponent(localUrl);
//     oHead.appendChild(oScript);
// })();
// js回调函数
window.callback = function (data_) {
        let data={};
        if (typeof(data_) === "string") {
                try {
                    data = eval("("+data_+")");
                } catch (e) {
                    return data_;
                }
            } else {
             data = data_;
        }
    wx.config({
        debug: false,
        appId: data.appid,
        timestamp: data.timestamp,
        nonceStr: data.nonceStr,
        url: data.url,
        signature: data.signature,
        jsApiList: ['hideOptionMenu', 'onMenuShareAppMessage', 'onMenuShareTimeline','showMenuItems']
    });
    wx.ready(function () {
        //隐藏右上角菜单接口
        wx.hideOptionMenu();
    });
}

const wechat_ = {
    /**
     * [getWechatToken 获取微信token]
     * @return {[type]} [description]
     */
    getWechatToken() {
        const _appid = wechat._appid,
            _redirect_sys = encodeURIComponent(location.href.replace(location.search, "")),
            _redirect = wechat.redirect_uri;
        window.location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + _appid + '&redirect_uri=' + _redirect + '%3FuserType%3DWECHATUSER%26redirectUrl%3D' + _redirect_sys + '&response_type=code&scope=snsapi_base&state=abottt#wechat_redirect');
    },
    /**
     * [getWechatUserInfo_ 获取微信框架用户信息带头像名称]
     * @param  {[type]}   token    [description]
     * @param  {Function} callback [description]
     * @return {[type]}            [description]
     */
    getWechatUserInfo_() {
        const _appid = wechat._appid,
            _redirect_sys = encodeURIComponent(location.href.replace(location.search, "")),
            _redirect = wechat.redirect_uri;
        window.location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + _appid + '&redirect_uri=' + _redirect + '%3FuserType%3DWECHATUSER%26redirectUrl%3D' + _redirect_sys + '&response_type=code&scope=snsapi_userinfo&state=abottt#wechat_redirect');
    },
    /**
     * [getWechatUserInfo 获取微信框架用户信息]
     * @param  {[type]}   token    [description]
     * @param  {Function} callback [description]
     * @param  {Function} WechatUserInfo [description]
     * @return {[type]}            [description]
     */
    getWechatUserInfo(token, callback, WechatUserInfo) {
        jsonp(wechat.swechatframework_url + '?t=' + Math.random() + '&userType=WECHATUSER&token=' + token, null, function (err, data) {
            if (err) {
                alert('网络异常');
            } else {
                console.log(data);
                try {
                    if (data != null) {
                        // 如果ada不存在则代表解绑
                        if (!data.ada) {
                            api.storage.removeSession('_WECHAT_TOKEN');
                            window.location.href = wechat.redirect_ada + '?sid=' + wechat.sid + '&uid=' + data.openid + '&type=2';
                        } else {
                            //如果头像未取回来 没关注同授权就进行授权
                            if(data.headimgurl==""){
                                api.storage.removeSession('_WECHAT_TOKEN');
                                api.storage.removeSession('_WECHAT_TOKEN');
                                wechat_.getWechatUserInfo_();
                                return;
                            }
                            api.storage.setLocal('_WECHAT_TOKEN', token);
                            api.storage.setSession('_WECHAT_TOKEN', token);
                            if (typeof callback === "function") {
                                callback.call(this, data)
                            }
                        }
                    } else {
                        // 如果获取不到用户信息，则代表过期重新获取,清除本地token
                        api.storage.removeLocal('_WECHAT_TOKEN');
                        api.storage.removeSession('_WECHAT_TOKEN');
                        WechatUserInfo();
                    }
                } catch (e) {
                    // 授权异常的情况下关闭当前页
                    alert('获取微信认证信息失败，请稍候重试！');
                }
            }
        });
    }
}

export default {
    wechat_,
    wechat
}
